﻿using System.Collections.Generic;
using System.Data;
using Ue.Infrastructure;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.Membership;
using Ue.Qcdz.Domain.SaleInfos;
using Ue.Qcdz.Domain.Shops;
namespace Ue.Qcdz.Repositories.Shops
{
    public class ShopRepository : SqlServerRepository<Shop>, IShopRepository
    {

        public ShopRepository()
            : this(null) { }

        public ShopRepository(IUnitOfWork uniteOfWork)
            : base(uniteOfWork) { }



        protected override System.Data.IDataParameter[] BuildParameters(Shop entity)
        {
            //
            entity.Remark += "|" + entity.Linkman;
            entity.Remark += "|" + entity.CompanyAddress;
            entity.Remark += "|" + entity.Postcode;
            entity.Remark += "|" + entity.Telephone;
            entity.Remark += "|" + entity.Mobilephone;
            entity.Remark += "|" + entity.Fax;
            entity.Remark += "|" + entity.Email;
            entity.Remark += "|" + entity.MSN;
            entity.Remark += "|" + entity.QQ;
            entity.Remark += "|" + entity.SiteUrl;
            entity.Remark += "|" + entity.CompanyName;
            entity.Remark += "|" + entity.RegisteredCapital;
            entity.Remark += "|" + entity.LegalPerson;
            entity.Remark += "|" + entity.Employees;
            entity.Remark += "|" + entity.EstablishingTime;
            entity.Remark += "|" + entity.CompanyProfiles;
            //
            return new IDataParameter[]{
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.EntityKey,entity.Key),
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.Name,entity.Name??""),              
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.LogoUrl,entity.LogoUrl??""),
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.KeyWords,entity.KeyWords??""),
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.Description,entity.Description??""),  
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.Status,entity.Status),              
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.Remark,entity.Remark??""),
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.UserId,entity.User.Key),
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.IsTop,entity.IsTop),
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.CreateOn,entity.CreateOn),
              this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.Banner,entity.Banner)
            };
        }

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            if (dataQuery is IShopDataQuery)
            {
                return new ShopDataQueryWhereClause(dataQuery as IShopDataQuery);
            }

            return null;
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[] {
                    ShopFactory.FieldNames.EntityKey,
                    ShopFactory.FieldNames.Name,
                    ShopFactory.FieldNames.LogoUrl,
                    ShopFactory.FieldNames.KeyWords,
                    ShopFactory.FieldNames.Description,
                    ShopFactory.FieldNames.IsTop,
                    ShopFactory.FieldNames.Remark,
                    ShopFactory.FieldNames.Status, 
                    ShopFactory.FieldNames.UserId,
                    ShopFactory.FieldNames.CreateOn,
                    ShopFactory.FieldNames.Banner
            };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[] {
                    ShopFactory.FieldNames.Name,
                    ShopFactory.FieldNames.LogoUrl,
                    ShopFactory.FieldNames.KeyWords,
                    ShopFactory.FieldNames.Description,
                    ShopFactory.FieldNames.IsTop,
                    ShopFactory.FieldNames.Remark,
                    ShopFactory.FieldNames.Status,
                    ShopFactory.FieldNames.Banner
            };
        }

        protected override IDictionary<string, SqlServerRepositoryBase<Shop>.ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            var callBacks = new Dictionary<string, SqlServerRepository<Shop>.ChildDataHandler>();
            callBacks.Add(ShopFactory.FieldNames.UserId, delegate(Shop shop, object fieldValue)
            {
                ShopFroxy shopProxy = shop as ShopFroxy;
                if (shopProxy != null)
                {
                    IUserRepository userRepository = RepositoryFactory.GetRepository<IUserRepository, User>();
                    shop.User = userRepository[fieldValue];

                }
            });
            return callBacks;
        }

        protected override string GetOrderBy()
        {
            return "CreateOn DESC";
        }

        protected override string GetDataTableName()
        {
            return "Shop";
        }

        protected override string GetKeyFieldName()
        {
            return "id";
        }

        #region IShopRepository 成员

        public Shop GetShop(Domain.Membership.User user)
        {
            if (user == null) return null;
            string sql = "SELECT * FROM Shop WHERE UserId=@UserId";
            var parameter = new IDataParameter[]
                                             {
                                                 this.RepositoryDriver.BuildParameter(ShopFactory.FieldNames.UserId,
                                                                                      user.Key)
                                                               
            };
            return BuildEntityFromSql(sql, parameter);
        }
        #endregion
    }

}